package com.itheima.ui;

import cn.hutool.core.io.FileUtil;
import com.itheima.domain.User;
import com.itheima.util.CodeUtil;

import javax.swing.*;
import java.awt.event.*;
import java.io.IOException;
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.List;

public class LoginJFrame extends JFrame implements MouseListener {
    ArrayList<User> allUsers = new ArrayList<>();
    JButton login = new JButton();
    JButton register = new JButton();
    JTextField username = new JTextField();
    JPasswordField password = new JPasswordField();
    JTextField code = new JTextField();
    JLabel rightCode = new JLabel();

    public LoginJFrame() {
        readUserInfo();

        initJFrame();

        initView();

        this.setVisible(true);
    }

    private void readUserInfo() {
        List<String> list = FileUtil.readUtf8Lines("D:\\java\\src\\com\\jyu\\mypuzzle\\userinfo.txt");
        for (String s : list) {
            String[] strings = s.split("&");
            String[] split1 = strings[0].split("=");
            String[] split2 = strings[1].split("=");
            User u = new User(split1[1], split2[1]);
            this.allUsers.add(u);
        }
    }

    private void initView() {
        JLabel usernameText = new JLabel(new ImageIcon("image\\login\\用户名.png"));
        usernameText.setBounds(116, 135, 47, 17);
        this.getContentPane().add(usernameText);

        this.username.setBounds(185, 128, 200, 30);
        this.getContentPane().add(this.username);

        JLabel passwordText = new JLabel(new ImageIcon("image\\login\\密码.png"));
        passwordText.setBounds(130, 195, 32, 16);
        this.getContentPane().add(passwordText);

        this.password.setBounds(185, 190, 200, 30);
        this.getContentPane().add(this.password);

        JLabel codeText = new JLabel(new ImageIcon("image\\login\\验证码.png"));
        codeText.setBounds(116, 250, 50, 30);
        this.getContentPane().add(codeText);

        this.code.setBounds(185, 252, 100, 30);
        this.code.addMouseListener(this);
        this.getContentPane().add(this.code);

        String codeStr = CodeUtil.getCode();
        this.rightCode.setText(codeStr);
        this.rightCode.addMouseListener(this);

        this.rightCode.setBounds(300, 256, 50, 30);
        this.getContentPane().add(this.rightCode);

        this.login.setBounds(123, 310, 128, 47);
        this.login.setIcon(new ImageIcon("image\\login\\登录按钮.png"));
        this.login.setBorderPainted(false);
        this.login.setContentAreaFilled(false);
        this.login.addMouseListener(this);
        this.getContentPane().add(this.login);

        this.register.setBounds(256, 310, 128, 47);
        this.register.setIcon(new ImageIcon("image\\login\\注册按钮.png"));
        this.register.setBorderPainted(false);
        this.register.setContentAreaFilled(false);
        this.register.addMouseListener(this);
        this.getContentPane().add(this.register);

        JLabel background = new JLabel(new ImageIcon("image\\login\\background.png"));
        background.setBounds(2, 0, 470, 390);
        this.getContentPane().add(background);
    }

    private void initJFrame() {
        this.setSize(488, 430);
        this.setTitle("拼图单机版 v1.0登录");
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setAlwaysOnTop(true);
        this.setLayout(null);

    }


    @Override
    public void mouseClicked(MouseEvent e) {
        Object obj = e.getSource();
        String changeCode;
        if (obj == login) {
            System.out.println("点击了登录");
            String usernameInput = this.username.getText();
            char[] password = this.password.getPassword();
            String codeInput = this.code.getText();
            System.out.println("用户输入的用户名为" + usernameInput);
            String passwordInput = new String(password);
            User userInfo = new User(usernameInput, passwordInput);
            System.out.println("用户输入的密码为" + passwordInput);
            if (codeInput.length() == 0) {
                this.showJDialog("验证码不能为空");
            } else if (usernameInput.length() == 0) {
                this.showJDialog("用户名不能为空");
            } else if (passwordInput.length() == 0) {
                this.showJDialog("密码不能为空");
            } else {
                if (!codeInput.equalsIgnoreCase(this.rightCode.getText())) {
                    this.showJDialog("验证码输入错误");
                } else if (this.contains(userInfo)) {
                    System.out.println("用户名和密码正确可以开始玩游戏了");
                    this.setVisible(false);
                    try {
                        new GameJFrame();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    } catch (ClassNotFoundException ex) {
                        ex.printStackTrace();
                    }
                } else {
                    System.out.println("用户名或密码错误");
                    this.showJDialog("用户名或密码错误");
                }
            }
        } else if (obj == register) {
            System.out.println("点击了注册");
            this.setVisible(false);
            new RegisterJFrame(allUsers);
        } else if (obj == this.rightCode) {
            System.out.println("更换验证码");
            changeCode = CodeUtil.getCode();
            this.rightCode.setText(changeCode);
        }

    }

    private boolean contains(User userInfo) {
        for (int i = 0; i < this.allUsers.size(); i++) {
            User rigehtuser = this.allUsers.get(i);
            String rightusername = rigehtuser.getUsername();
            String rightpassword = rigehtuser.getPassword();
            if (rightusername.equals(userInfo.getUsername()) &&
                    rightpassword.equals(userInfo.getPassword())) {
                return true;
            }
        }
        return false;
    }

    private void showJDialog(String content) {
        JDialog jDialog = new JDialog();
        jDialog.setSize(200, 150);
        jDialog.setAlwaysOnTop(true);
        jDialog.setLocationRelativeTo(null);
        jDialog.setModal(true);
        JLabel jLabel = new JLabel(content);
        jLabel.setBounds(0, 0, 200, 150);
        jDialog.getContentPane().add(jLabel);
        jDialog.setVisible(true);
    }

    @Override
    public void mousePressed(MouseEvent e) {
        if (e.getSource() == login) {
            login.setIcon(new ImageIcon("image\\login\\登录按下.png"));
        } else if (e.getSource() == register) {
            register.setIcon(new ImageIcon("image\\login\\注册按下.png"));
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        if (e.getSource() == login) {
            login.setIcon(new ImageIcon("image\\login\\登录按钮.png"));
        } else if (e.getSource() == register) {
            register.setIcon(new ImageIcon("image\\login\\注册按钮.png"));
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
